package com.class05.lambda;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
 * @ClassDescription:
 * 1.Lambda表达式的使用举例：
 * (o1,o2)->Integer.compare(o1,o2);
 * 2.Lambda表达式的格式举例：
 * lambda形参列表->lambda体
 * 3.Lambda表达式的格式
 *
 *
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/28 8:35
 */
public class LambdaTest1 {
    @Test//语法格式1：无参无返回值
    public void test1() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Lambda好学吗？");
            }
        };
        runnable.run();
        System.out.println("=================================");
        Runnable r1 = () -> {
            System.out.println("Lambda挺简单的");
        };
        r1.run();
    }

    @Test//语法格式2：需要一个参数，但是无返回值
    public void test2() {
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("s = " + s);
            }
        };
        con.accept("谎言和誓言的区别是什么？");

        System.out.println("=================================");

        Consumer<String> con1 = (String s) -> {
            System.out.println("s = " + s);
        };
        con1.accept("一个说的人当真了，一个听的人当真了");
    }


    @Test//语法格式3：数据类型可以省略，因为可由编译器推断得出，称为类型“类型推断”
    public void test3() {

        Consumer<String> con1 = (String s) -> {
            System.out.println("s = " + s);
        };
        con1.accept("如果大学可以重来，你最想干啥？");
        System.out.println("=================================");
        Consumer<String> con2 = (s) -> {
            System.out.println("s = " + s);
        };
        con2.accept("谈一场轰轰烈烈的爱情");
    }

    @Test
    public void test3_1() {
        int[] arr = {1, 2, 3, 4};//类型推断
        HashMap<String, Integer> map = new HashMap<>();//类型推断
//        类型推断，在jdk10及以后可以用
        var entrySet = map.entrySet();
    }

    @Test//语法格式4：Lambda 若只需要一个参数时，参数的小括号可以省略
    public void test4() {
        Consumer<String> con2 = (s) -> {
            System.out.println("s = " + s);
        };
        con2.accept("谈一场轰轰烈烈的爱情");
        System.out.println("=================================");
        Consumer<String> con3 = s -> {
            System.out.println("s = " + s);
        };
        con2.accept("谈一场轰轰烈烈的爱情");
    }

    @Test//语法格式5：Lambda 需要两个或以上的参数，多条执行语句，并可以有返回值
    public void test5() {
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };
        System.out.println("com1.compare(12,21) = " + com1.compare(12, 21));

        System.out.println("=================================");


        Comparator<Integer> com2 = (Integer o1, Integer o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println("com2.compare(1,2) = " + com2.compare(1, 2));

        System.out.println("=================================");

        Comparator<Integer> com3 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println("com2.compare(1,2) = " + com2.compare(1, 2));

    }

    @Test//语法格式六：当lambda体只有一条语句时，return与大括号如有，都可以省略
    public void test6() {
        Comparator<Integer> com1 = (o1, o2) -> {
            return o1.compareTo(o2);
        };
        System.out.println("com1.compare(12,6) = " + com1.compare(12, 6));

        Comparator<Integer> com2 = (o1, o2) ->
                o1.compareTo(o2);
        int compare = com2.compare(12, 5);
        System.out.println("compare = " + compare);


    }

    @Test
    public void test7() {
        Consumer<String> con2 = s ->
                System.out.println("s = " + s);
        con2.accept("谈一场轰轰烈烈的爱情");
    }


}
